<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework                                                   |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify  |
| it under the terms of the GNU General Public License as published by  |
| the Free Software Foundation. You should have received a copy of the  |
| GNU General Public License along with this program.  If not, see      |
| http://www.gnu.org/licenses/.                                         |
| Copyright (C) 2008-2009. All Rights Reserved.                         |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com                                    |
+-----------------------------------------------------------------------+
}}}*/
/**
 * @package SlightPHP
 */
namespace plug;

class SHttp{
    /**
    * curl post 默认get提交
    * 
    * @param mixed $url
    * @param mixed $var
    * @param mixed $timeout
    * @param mixed $type
    * @param mixed $referer
    * @return string
    */
    public static function http_curl($url, $var="", $theHeaders=array(), $type = 'get', $timeout = 120, $referer = ''){
        $curl = curl_init(); 

        curl_setopt($curl, CURLOPT_URL, $url); 
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);

        curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
        curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
        if(!empty($theHeaders)){
            curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
            curl_setopt($curl, CURLOPT_SSLCERT, '/data/wwwroot/xiaozaopai/service/serial/apiclient_cert.pem');
            curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
            curl_setopt($curl, CURLOPT_SSLKEY, '/data/wwwroot/xiaozaopai/service/serial/apiclient_key.pem');

            curl_setopt($curl, CURLOPT_HTTPHEADER, $theHeaders);
        }
        if(!empty($referer)){
            curl_setopt($curl, CURLOPT_REFERER, $referer);
        }
        if($type == 'post'){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $var);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); 

        $data['str'] = curl_exec($curl); 
        $data['status'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $data['errno'] = curl_error($curl);

        curl_close($curl);  

        return $data;
    }
}
?>
